package cate.game.play.skill.passive.awaken;

import cate.common.util.XT;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.proce.action.ctx.TimeActionCtx;
import cate.game.play.proce.move.MoveCtx;
import cate.game.play.skill.passive.PassiveHandler;

/**
 *  若觉醒状态下当前回合未出手,则下回合出手时对敌方3个目标造成350%灵能攻击的真实伤害 （被动脚本）
 *  觉醒状态下攻击时若目标闪避,提升自身25%觉醒值,若目标格挡,50%概率降低目标25%觉醒值,每次至多2个目标（被动脚本）
 */
public class 风神的祝福PH extends PassiveHandler {

	private int 伤害目标;

	private int 伤害buff;

	private int 闪避提升觉醒;

	private int 格挡概率;

	private int 降低觉醒;

	private int 最大目标;

	//伤害目标=3&伤害buff=？？&闪避提升觉醒=25&格挡概率=5000&降低觉醒=25&最大目标=2
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		伤害目标 = args.getInt("伤害目标", 0);
		伤害buff = args.getInt("伤害buff", 0);
		闪避提升觉醒 = args.getInt("闪避提升觉醒", 0);
		格挡概率 = args.getInt("格挡概率", 0);
		降低觉醒 = args.getInt("降低觉醒", 0);
		最大目标 = args.getInt("最大目标", 0);
	}

	private int lastMoveRound;
	private int damageRound;

	private int reduceNum;

	@Override
	public void onOwnerMoveDone(MoveCtx move, TimeActionCtx action) {
		lastMoveRound = move.getRound().index;
		if (damageRound == move.getRound().index) {
			TargetFilterParam param = new TargetFilterParam().targetNumSet(伤害目标);
			for (Fighter target : TargetSelector.doFind(skill.owner, action.getTeamVs(), param)) {
				target.buff.tryAddByTid(action, skill.owner, 伤害buff);
			}
			damageRound = 0;
		}
		reduceNum = 0;
	}

	@Override
	public void onRoundEnd(ActionCtx action) {
		if (lastMoveRound == action.getRound().index || !skill.owner.status.isAwaken()) {
			return;
		}
		damageRound = action.getRound().index  + 1;
	}

	@Override
	public void onMissAsActor(SkillActionCtx action, Fighter target) {
		if (!skill.owner.status.isAwaken()) {
			return;
		}
		if (闪避提升觉醒 <= 0) {
			return;
		}
		skill.owner.skill.addAwakenEnergy(action, 闪避提升觉醒);
	}

	@Override
	public void afterOneAttack(SkillActionCtx action, long totalDamage, boolean strike, Fighter target) {
		if (skill.owner.isDead() || !skill.owner.status.isAwaken()) {
			return;
		}
		if (!action.getStat().isParry(target.getPid())) {
			return;
		}
		if (!XT.chance(格挡概率)) {
			return;
		}
		if (reduceNum >= 最大目标) {
			return;
		}
		boolean success = target.skill.addAwakenEnergy(action, -降低觉醒);
		if (success) {
			reduceNum++;
		}
	}
}
